<?php

class My_Plugin_SEO extends Zend_Controller_Plugin_Abstract {

    /**
     * Called after the router finishes routing the request.
     * @param Zend_Controller_Request_Abstract $request
     */
    public function postDispatch(Zend_Controller_Request_Abstract $request) {
        //Get module
        $module = strtolower($request->getParam('module', 'frontend'));

        //Get controller
        $controller = strtolower($request->getParam('controller'));

        //Get action
        $action = strtolower($request->getParam('action'));

        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        if (null === $viewRenderer->view) {
            $viewRenderer->initView();
        }
        $view = $viewRenderer->view;

        $strCurrentUrl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getRequestUri();

        $instanceSEO = SEO::getInstance();
        if ($arrSEO = $instanceSEO->getDetail(md5($strCurrentUrl))) {
            $keyword = $arrSEO['seo_keywords'];
            $description = $arrSEO['seo_description'];
            $title = $arrSEO['seo_title'];
        }

        if (empty($arrSEO)) {
            $resource = $module . ":" . $controller . ":" . $action;
            $page = ($request->page) ? $request->page : 1;
            switch ($resource) {
                default :
                    $title = "Deal, Voucher, Coupon, Groupon | VnE Go Chợ DEAL";
                    $keyword = "Deal, Voucher, Coupon, Groupon, gia re, tp hcm, ha noi, toan quoc, giao hang, du lich, giai tri, suc khoe, spa, nghi duong, an uong, di choi";
                    $description = "Deal giá rẻ, Voucher giá rẻ, Coupon giá rẻ, Groupon giá rẻ tổng hợp từ các Deal Site. Giá rẻ, Hàng tốt, Rẻ hàng ngày, Dễ thanh toán";
                    break;
            }
        }

        $view->headTitle($title);
        if ($keyword) {
            $view->headMeta()->setName('keywords', $keyword);
        }
        if ($description) {
            $view->headMeta()->setName('description', $description);
        }
    }

}

